package com.yitao.cms.service.shopInfo;

import com.skipper.base.data.querydsl.PredicateBuilder;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.constant.ShopEnums;
import com.yitao.client.dto.shopInfo.ShopPaymentMethodDto;
import com.yitao.client.model.shopInfo.QShopPaymentMethodModel;
import com.yitao.client.model.shopInfo.ShopPaymentMethodModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺支付方式
 *
 * @Date: 2020/4/14 11:56
 * @Author: Kien
 */
@Service
public class ShopPaymentMethodService extends GenericEntityService<ShopPaymentMethodModel, ShopPaymentMethodDto, QShopPaymentMethodModel> {

    public ShopPaymentMethodService(EntityManager entityManager) {
        super(entityManager);
    }

    /**
     * 功能描述: 根据店铺id获取店铺的支付方式
     *
     * @param shopId 店铺id
     *
     * @return
     * @auther Kien
     * @date 2020/4/14 11:58
     */
    public List<Integer> findShopPaymentMethodByShopId(Long shopId){
        if(shopId == null){
            return null;
        }
        List<Integer> list = new ArrayList<>();
        List<ShopPaymentMethodModel> all = repository.findAll(PredicateBuilder.conjunction()
                .and(() -> Q.shopId.eq(shopId))
                .and(() -> Q.deleteStatus.eq(false))
                .get());
        if(all != null && all.size() != 0) {
            list = all.stream().map(it -> it.getPaymentCode()).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * 功能描述: 店铺新增或者修改支付方式
     *
     * @param paymentCodeList 支付方式列表
     * @param shopId
     * @return
     * @auther Kien
     * @date 2020/4/14 14:36
     */
    @Transactional
    public int addPayment(List<Integer> paymentCodeList, Long shopId, Long userId) {
        if(shopId == null){
            return 0;
        }
        if(paymentCodeList == null || paymentCodeList.size() == 0) {
            paymentCodeList = new ArrayList<>();
            paymentCodeList.add(1);
            paymentCodeList.add(2);
        }
        // 先将旧的删除
        repository.updateByWhere(PredicateBuilder.conjunction()
                .and(() -> Q.shopId.eq(shopId))
                .and(() -> Q.deleteStatus.eq(false))
                .get(), it -> it.set(Q.deleteStatus, true));
        // 新增
        Date date = new Date();
        List<ShopPaymentMethodModel> list = new ArrayList<>();
        for(Integer payCode: paymentCodeList){
            ShopPaymentMethodModel shopPaymentMethodModel = new ShopPaymentMethodModel();
            shopPaymentMethodModel.setShopId(shopId);
            shopPaymentMethodModel.setPaymentCode(payCode);
            shopPaymentMethodModel.setPaymentMethod(ShopEnums.SHOP_PAYMENT_METHOD.getNameByCode(payCode));
            shopPaymentMethodModel.setDeleteStatus(false);
            shopPaymentMethodModel.setCreateBy(userId);
            shopPaymentMethodModel.setCreateTime(date);
            shopPaymentMethodModel.setLastUpdateBy(userId);
            shopPaymentMethodModel.setLastUpdateTime(date);
            list.add(shopPaymentMethodModel);
        }
        if(list != null && list.size() != 0) {
            List<ShopPaymentMethodModel> save = repository.save(list);
            return save.size();
        }
        return 0;
    }
}
